let App = getApp();

Page({
  data: {
    username:'',
    password:'',
    recommendId:0,
      },

  onLoad: function (params) {
    console.info("loginOption", params);
    if(params.recommendId != null){
      App.globalData.recommendId = params.recommendId;
    }
    console.info(" App.globalData.recommendId", App.globalData.recommendId);
    // wx.getShareInfo({
    //   shareTicket: params.shareTicket,
    //   success:function(res){
    //     var encryptedData = res.encryptedData;
    //     var iv = res.iv;
    //     console.info("encryptedData",encryptedData);
    //   }
    // })
    var me = this;
  
   
    var redirectUrl = params.redirectUrl;
    // debugger;
    if (redirectUrl != null && redirectUrl != undefined && redirectUrl != '') {
      redirectUrl = redirectUrl.replace(/#/g, "?");
      redirectUrl = redirectUrl.replace(/@/g, "=");

      me.redirectUrl = redirectUrl;
    }
  },
 
  getPhone:function(e){
    let _this = this;
    _this.data.username = e.detail.value;
   
   
  },
  getPwd:function(e){
    let _this = this;
    _this.data.password = e.detail.value;
    
  },
  // 登录  
  doLogin: function (e) {
    let _this = this;
    if (e.detail.errMsg !== 'getUserInfo:ok') {
      return false;
    }
  
    var username = _this.data.username;
    var password = _this.data.password;
    console.info("username", username);

    console.info("e", e);
    console.info("password", password);
    if (username.length == 0 || password.length == 0) {
      wx.showToast({
        title: '用户名或密码不能为空',
        icon: 'none',
        duration: 3000
      })
    } else {
    wx.showLoading({ title: "正在登录", mask: true });
    // 执行微信登录
    wx.login({
      success: function (res) {
        // 发送用户信息
        App._post_form('user/userLogin'
          , {
            username:username,
            password:password,
            code: res.code,
            user_info: e.detail.rawData,
            encrypted_data: e.detail.encryptedData,
            iv: e.detail.iv,
            signature: e.detail.signature
          }
          , function (result) {
            // 记录token user_id
            if(result.status==200){
            console.info("success", result.data.session_key);
            console.info("result.data.user_id", result.data.user_id);
            wx.setStorageSync('token', result.data.session_key);
            wx.setStorageSync('user_id', result.data.user_id);
            wx.setStorageSync('role', result.data.role);
            App.globalData.role = result.data.role;
            wx.switchTab({
              url: '/pages/index/index',
              fail: function () {
                console.info("跳转失败")
              }
            })
            }else{
              wx.showToast({
                title: '用户名或密码错误',
                icon: 'none',
                duration: 3000
              })
              wx.hideLoading();
            }
          }
          , false
          , function () {
           
          });
      }
    });
    }
  },
goWxLogin:function(){
  wx.redirectTo({
    url: '../login/login',
  })
},
  goRegistPage: function () {
    wx.redirectTo({
      url: '../regist/regist?recommendId='+App.globalData.recommendId,
    })
  },
  goRegistPageForDoctor: function () {
    wx.redirectTo({
      url: '../regist/registForDoctor',
    })
  },
  goRegistPageForOrg: function () {
    wx.redirectTo({
      url: '../regist/registForOrg',
    })
  }
})